﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using IPC.Communication.Framework.Core;
using IPC.Communication.Framework.Core.Net;
using IPC.Communication.Framework.Profinet.Melsec.Helper;
using IPC.Communication.Framework.Reflection;
using IPC.Communication.Framework.Core.Types;

namespace IPC.Communication.Framework.Profinet.Melsec
{
    public class MelsecA3CNetOverTcp : NetworkDeviceBase, IReadWriteA3C, IReadWriteDevice, IReadWriteNet
    {
        private byte station = 0;
        private bool _sumCheck = true;
        private int _format = 1;

        public byte Station
        {
            get
            {
                return station;
            }
            set
            {
                station = value;
            }
        }

        public bool SumCheck
        {
            get { return _sumCheck; }
            set { _sumCheck = value; }
        }


        public int Format
        {
            get { return _format; }
            set { _format = value; }
        }


        public MelsecA3CNetOverTcp()
        {
            base.WordLength = 1;
            base.ByteTransform = new RegularByteTransform();
            base.SleepTime = 20;
        }

        public MelsecA3CNetOverTcp(string ipAddress, int port)
            : this()
        {
            IpAddress = ipAddress;
            Port = port;
        }

        [HslMqttApi("ReadByteArray", "")]
        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            return MelsecA3CNetHelper.Read(this, address, length);
        }

        [HslMqttApi("WriteByteArray", "")]
        public override OperateResult Write(string address, byte[] value)
        {
            return MelsecA3CNetHelper.Write(this, address, value);
        }

        //[AsyncStateMachine(typeof(<ReadAsync>d__15))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
        //{
        //    <ReadAsync>d__15 stateMachine = new <ReadAsync>d__15();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__16))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, byte[] value)
        //{
        //    <WriteAsync>d__16 stateMachine = new <WriteAsync>d__16();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}


        [HslMqttApi("ReadBoolArray", "")]
        public override OperateResult<bool[]> ReadBool(string address, ushort length)
        {
            return MelsecA3CNetHelper.ReadBool(this, address, length);
        }

        [HslMqttApi("WriteBoolArray", "")]
        public override OperateResult Write(string address, bool[] value)
        {
            return MelsecA3CNetHelper.Write(this, address, value);
        }

        //[AsyncStateMachine(typeof(<ReadBoolAsync>d__19))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
        //{
        //    <ReadBoolAsync>d__19 stateMachine = new <ReadBoolAsync>d__19();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__20))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, bool[] value)
        //{
        //    <WriteAsync>d__20 stateMachine = new <WriteAsync>d__20();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi]
        public OperateResult RemoteRun()
        {
            return MelsecA3CNetHelper.RemoteRun(this);
        }

        [HslMqttApi]
        public OperateResult RemoteStop()
        {
            return MelsecA3CNetHelper.RemoteStop(this);
        }

        [HslMqttApi]
        public OperateResult<string> ReadPlcType()
        {
            return MelsecA3CNetHelper.ReadPlcType(this);
        }

        //[AsyncStateMachine(typeof(<RemoteRunAsync>d__24))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> RemoteRunAsync()
        //{
        //    <RemoteRunAsync>d__24 stateMachine = new <RemoteRunAsync>d__24();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<RemoteStopAsync>d__25))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> RemoteStopAsync()
        //{
        //    <RemoteStopAsync>d__25 stateMachine = new <RemoteStopAsync>d__25();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadPlcTypeAsync>d__26))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<string>> ReadPlcTypeAsync()
        //{
        //    <ReadPlcTypeAsync>d__26 stateMachine = new <ReadPlcTypeAsync>d__26();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        public override string ToString()
        {
            return String.Format("MelsecA3CNetOverTcp[{0}:{1}]", IpAddress, Port);
        }
    }
}
